package com.twb.core.money;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.twb.core.util.MoneyUtil;
import java.io.IOException;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.jackson.JsonComponent;


/**
 * 金额反序列化转换
 * 元转分
 *
 * @author DHL
 * @date 2022-10-10 12:29:22
 */
@JsonComponent
public class MoneyDeSerializer extends JsonDeserializer<Long> {

  @Override
  public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
    String value = p.getText();
    if(StringUtils.isBlank(value)){
      value = "0";
    }
    // 元转分
    return MoneyUtil.Y2F(new BigDecimal(value));
  }
}
